package dk.itu.amds.auctionhouse.utility;

import java.io.Serializable;

import org.pi4soa.service.LockedInformationException;
import org.pi4soa.service.ServiceException;
import org.pi4soa.service.VariableUnavailableException;
import org.pi4soa.service.extensions.ExtensionContext;

import dk.itu.amds.auctionhouse.domain.AuctionItem;

public class MessageHandler {

	private static int getUserId(ExtensionContext context) throws VariableUnavailableException, LockedInformationException, ServiceException {
		int userId = -1;

		try {
			userId = Integer.parseInt(context.getVariable("userId").toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return userId;

	}

	public static Serializable getMessageXml(MessageType messageType,
			ExtensionContext context) throws VariableUnavailableException, LockedInformationException, ServiceException {
		int userId = getUserId(context);
		Serializable xml = null;

		switch (messageType) {
		case SELL_RESPONSE_SUCCESS:
			xml = getGenericResponse(userId, "sellResponse", Constants.STATUS_SUCCESS);
			break;
		case SELL_RESPONSE_FAILURE:
			xml = getGenericResponse(userId, "sellResponse", Constants.STATUS_FAILURE);
			break;
		case AH_PAY_REQUEST:
			xml = getAuctionHousePayRequest(userId, context);
			break;
		case AH_PAY_RESPONSE_FAILURE:
			xml = getGenericResponse(userId, "AuctionHousePayResponse", Constants.STATUS_FAILURE);
			break;
		case AH_PAY_RESPONSE_SUCCESS:
			xml = getGenericResponse(userId, "AuctionHousePayResponse", Constants.STATUS_SUCCESS);
			break;
		case BID_RESPONSE_MAX_BID_REACHED:
			xml = getGenericResponse(userId, "BidResponse", Constants.STATUS_MAX_BID_REACHED);
			break; 
		case BID_RESPONSE_MAX_BID_NOT_REACHED:
			xml = getGenericResponse(userId, "BidResponse", Constants.STATUS_MAX_BID_NOT_REACHED);
			break;
		case BID_RESPONSE_CURRENT_BID_NOT_REACHED:
			xml = getGenericResponse(userId, "BidResponse", Constants.STATUS_CURRENT_BID_NOT_REACHED);
			break;
		case BID_RESPONSE_AUCTION_CLOSED:
			xml = getGenericResponse(userId, "BidResponse", Constants.STATUS_AUCTION_CLOSED);
			break;
		case BUYER_PAY_RESPONSE_SUCCESS:
			xml = getGenericResponse(userId, "BuyerPayResponse", Constants.STATUS_SUCCESS);
			break;
		case BUYER_PAY_RESPONSE_FAILURE:
			xml = getGenericResponse(userId, "BuyerPayResponse", Constants.STATUS_FAILURE);
			break;
		default:
			break;
		}
		
		return xml;
	}
	
	
	private static Serializable getAuctionHousePayRequest(int userId,
			ExtensionContext context) throws VariableUnavailableException, LockedInformationException, ServiceException {
		StringBuilder sb = new StringBuilder();
		
		AuctionItem auctionItem = (AuctionItem) context.getVariable("auctionItem");
		
		
		sb.append("<AuctionHousePayRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\\schemas\\AuctionHousePayRequest.xsd\" userId=\"" + userId + "\">");
		sb.append("<sellerBrokerAccountId>" + auctionItem.getSellerBrokerAccountId() + "</sellerBrokerAccountId>");
		sb.append("<buyerBrokerAccountId>" + context.getVariable("buyerBrokerAccountId") + "</buyerBrokerAccountId>");
		sb.append("<amount>" + context.getVariable("currentBid") + "</amount>");
		sb.append("</AuctionHousePayRequest>");
		
		return sb.toString();
	}

	private static Serializable getGenericResponse(int userId, String elementName, String status) {
		StringBuilder sb = new StringBuilder();
		
		sb.append("<" + elementName + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\\schemas\\" + elementName + ".xsd\"");
		sb.append(" userId=\"" + userId + "\" status=\"" + status + "\"/>");
		
		return sb.toString();
	}
}
